<!DOCTYPE html>
<html>
<head>
<title>oops!homework2.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

body {
	font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
	font-size: var(--vscode-markdown-font-size, 14px);
	padding: 0 26px;
	line-height: var(--vscode-markdown-line-height, 22px);
	word-wrap: break-word;
}

#code-csp-warning {
	position: fixed;
	top: 0;
	right: 0;
	color: white;
	margin: 16px;
	text-align: center;
	font-size: 12px;
	font-family: sans-serif;
	background-color:#444444;
	cursor: pointer;
	padding: 6px;
	box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}

#code-csp-warning:hover {
	text-decoration: none;
	background-color:#007acc;
	box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}

body.scrollBeyondLastLine {
	margin-bottom: calc(100vh - 22px);
}

body.showEditorSelection .code-line {
	position: relative;
}

body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
	content: "";
	display: block;
	position: absolute;
	top: 0;
	left: -12px;
	height: 100%;
}

body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
	left: -30px;
}

.vscode-light.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(0, 0, 0, 0.15);
}

.vscode-light.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(0, 0, 0, 0.40);
}

.vscode-light.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-dark.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 255, 255, 0.4);
}

.vscode-dark.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 255, 255, 0.60);
}

.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-high-contrast.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 160, 0, 0.7);
}

.vscode-high-contrast.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 160, 0, 1);
}

.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

img {
	max-width: 100%;
	max-height: 100%;
}

a {
	text-decoration: none;
}

a:hover {
	text-decoration: underline;
}

a:focus,
input:focus,
select:focus,
textarea:focus {
	outline: 1px solid -webkit-focus-ring-color;
	outline-offset: -1px;
}

hr {
	border: 0;
	height: 2px;
	border-bottom: 2px solid;
}

h1 {
	padding-bottom: 0.3em;
	line-height: 1.2;
	border-bottom-width: 1px;
	border-bottom-style: solid;
}

h1, h2, h3 {
	font-weight: normal;
}

table {
	border-collapse: collapse;
}

table > thead > tr > th {
	text-align: left;
	border-bottom: 1px solid;
}

table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
	padding: 5px 10px;
}

table > tbody > tr + tr > td {
	border-top: 1px solid;
}

blockquote {
	margin: 0 7px 0 5px;
	padding: 0 16px 0 10px;
	border-left-width: 5px;
	border-left-style: solid;
}

code {
	font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
	font-size: 1em;
	line-height: 1.357em;
}

body.wordWrap pre {
	white-space: pre-wrap;
}

pre:not(.hljs),
pre.hljs code > div {
	padding: 16px;
	border-radius: 3px;
	overflow: auto;
}

pre code {
	color: var(--vscode-editor-foreground);
	tab-size: 4;
}

/** Theming */

.vscode-light pre {
	background-color: rgba(220, 220, 220, 0.4);
}

.vscode-dark pre {
	background-color: rgba(10, 10, 10, 0.4);
}

.vscode-high-contrast pre {
	background-color: rgb(0, 0, 0);
}

.vscode-high-contrast h1 {
	border-color: rgb(0, 0, 0);
}

.vscode-light table > thead > tr > th {
	border-color: rgba(0, 0, 0, 0.69);
}

.vscode-dark table > thead > tr > th {
	border-color: rgba(255, 255, 255, 0.69);
}

.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
	border-color: rgba(0, 0, 0, 0.18);
}

.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
	border-color: rgba(255, 255, 255, 0.18);
}

</style>

<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */

/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
	color: #8e908c;
}

/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
	color: #c82829;
}

/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
	color: #f5871f;
}

/* Tomorrow Yellow */
.hljs-attribute {
	color: #eab700;
}

/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
	color: #718c00;
}

/* Tomorrow Blue */
.hljs-title,
.hljs-section {
	color: #4271ae;
}

/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
	color: #8959a8;
}

.hljs {
	display: block;
	overflow-x: auto;
	color: #4d4d4c;
	padding: 0.5em;
}

.hljs-emphasis {
	font-style: italic;
}

.hljs-strong {
	font-weight: bold;
}
</style>

<style>
/*
 * Markdown PDF CSS
 */

 body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
	padding: 0 12px;
}

pre {
	background-color: #f8f8f8;
	border: 1px solid #cccccc;
	border-radius: 3px;
	overflow-x: auto;
	white-space: pre-wrap;
	overflow-wrap: break-word;
}

pre:not(.hljs) {
	padding: 23px;
	line-height: 19px;
}

blockquote {
	background: rgba(127, 127, 127, 0.1);
	border-color: rgba(0, 122, 204, 0.5);
}

.emoji {
	height: 1.4em;
}

code {
	font-size: 14px;
	line-height: 19px;
}

/* for inline code */
:not(pre):not(.hljs) > code {
	color: #C9AE75; /* Change the old color so it seems less like an error */
	font-size: inherit;
}

/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
	page-break-after: always;
}

</style>

<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
  <script>
    mermaid.initialize({
      startOnLoad: true,
      theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
          ? 'dark'
          : 'default'
    });
  </script>
<h1 id="%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E7%9A%84%E9%A2%84%E5%A4%84%E7%90%86">心电信号的预处理</h1>
<h2 id="%E7%AC%AC%E4%BA%8C%E6%AC%A1%E4%BD%9C%E4%B8%9A">（第二次作业）</h2>
<h2 id="%E4%B8%80%E5%AF%B9%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E8%BF%9B%E8%A1%8C%E5%B9%B3%E6%BB%91%E6%BB%A4%E6%B3%A2">一、对心电信号进行平滑滤波</h2>
<h3 id="1%E7%A8%8B%E5%BA%8F">1.程序</h3>
<pre class="hljs"><code><div>    <span class="hljs-comment">%打开文件</span>
    fid1 = fopen(<span class="hljs-string">'G:\lqbzdwx\SmartHealth-master\SmartHealth-master\DATA\mitbd\100.dat'</span>);  
    ecg = fread(fid1,<span class="hljs-string">'short'</span>); <span class="hljs-comment">%以short型读取文件  </span>
    fclose(fid1);

    fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
    gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>
    t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间  </span>
    ecg = ecg/gain; <span class="hljs-comment">%去除信号自带增益？</span>

    <span class="hljs-comment">%平滑滤波器</span>
    ylabel(<span class="hljs-string">'幅度（mV）'</span>);
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    b = <span class="hljs-built_in">ones</span>(<span class="hljs-number">10</span>,<span class="hljs-number">1</span>)/<span class="hljs-number">20</span>;
    a = <span class="hljs-number">1</span>;
    t1 = filter(b,a,ecg);
    freqz(b,a);
    b = <span class="hljs-built_in">ones</span>(<span class="hljs-number">20</span>,<span class="hljs-number">1</span>)/<span class="hljs-number">20</span>;
    t2 = filter(b,a,ecg);
    <span class="hljs-built_in">figure</span>;
    freqz(b,a);
    <span class="hljs-built_in">figure</span>;
    <span class="hljs-built_in">hold</span> on;
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),t1(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),<span class="hljs-string">'-r'</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),t2(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),<span class="hljs-string">'-y'</span>);
    <span class="hljs-built_in">legend</span>(<span class="hljs-string">'ecg'</span>,<span class="hljs-string">'10阶平滑'</span>,<span class="hljs-string">'20阶平滑'</span>);
</div></code></pre>
<h3 id="2%E7%BB%93%E6%9E%9C%E5%9B%BE%E7%89%87">2.结果（图片）</h3>
<p><strong>平滑滤波前后对比</strong>
<img src="平滑滤波结果对比.png" alt="">
<strong>10阶平滑滤波频率响应特性图</strong>
<img src="10阶平滑滤波频率响应特性图.png" alt="">
<strong>20阶平滑滤波频率响应特性图</strong>
<img src="20阶平滑滤波频率响应特性图.png" alt=""></p>
<h2 id="%E4%BA%8C-%E5%AF%B9%E5%BF%83%E7%94%B5%E4%BF%A1%E5%8F%B7%E8%BF%9B%E8%A1%8C%E6%BB%A4%E6%B3%A2%E5%92%8C%E9%A2%84%E5%A4%87%E8%AF%86%E5%88%ABqrs%E6%B3%A2">二、 对心电信号进行滤波和预备识别QRS波</h2>
<h3 id="1-%E7%A8%8B%E5%BA%8F">1. 程序</h3>
<pre class="hljs"><code><div>    <span class="hljs-comment">%打开文件</span>
    fid1 = fopen(<span class="hljs-string">'G:\lqbzdwx\SmartHealth-master\SmartHealth-master\DATA\mitbd\100.dat'</span>);  
    ecg = fread(fid1,<span class="hljs-string">'short'</span>); <span class="hljs-comment">%以short型读取文件  </span>
    fclose(fid1);

    fs = <span class="hljs-number">250</span>; <span class="hljs-comment">%采样频率为250Hz  </span>
    gain = <span class="hljs-number">200</span>; <span class="hljs-comment">%增益为200  </span>
    t = (<span class="hljs-number">1</span>:<span class="hljs-built_in">length</span>(ecg))/fs; <span class="hljs-comment">%横坐标转为时间  </span>
    ecg = ecg/gain; <span class="hljs-comment">%去除信号自带增益？</span>

    <span class="hljs-comment">% for i=1:size(b,2) (for回圈说：你不要抛弃我啊球球了！！我：...哎呀，那好吧....(10min后)对不起我始乱终弃了</span>

    <span class="hljs-comment">%低通滤波：y(n)=2y(n-1)-y(n-2)+x(n)-2x(n-6)+x(n-12)</span>
    b = [<span class="hljs-number">1</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">-2</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">1</span>];
    a = [<span class="hljs-number">1</span> <span class="hljs-number">-2</span> <span class="hljs-number">1</span>];
    y1 = filter(b,a,ecg)/<span class="hljs-number">36</span>; <span class="hljs-comment">%低通滤波器增益是36倍</span>
    <span class="hljs-built_in">figure</span>;
    freqz(b,a);
    <span class="hljs-comment">%高通滤波：y(n)=y(n-1)+x(n)-x(n-32),z(n)=x(n-16)-y(n)/32 OR y(nT)=32x(nT-16T)-[y(nT-T)+x(nT)-x(nT-32T)]</span>
    b = [<span class="hljs-number">1</span> <span class="hljs-built_in">zeros</span>(<span class="hljs-number">1</span>,<span class="hljs-number">31</span>) <span class="hljs-number">-1</span>];
    a = [<span class="hljs-number">1</span>,<span class="hljs-number">-1</span>];
    y2 = filter(b,a,y1);
    b = [<span class="hljs-built_in">zeros</span>(<span class="hljs-number">1</span>,<span class="hljs-number">16</span>),<span class="hljs-number">1</span>];
    a = <span class="hljs-number">1</span>;
    y3 = filter(b,a,y1)-y2/<span class="hljs-number">32</span>; <span class="hljs-comment">%高通滤波器增益是32倍</span>
    <span class="hljs-comment">% b = [-1,zeros(1,16),32,zeros(1,16),1];</span>
    <span class="hljs-comment">% a = [1,1];</span>
    <span class="hljs-comment">% y3 = filter(b,a,y1)/32;</span>
    <span class="hljs-comment">%把信号差分来获取QRS波斜率信息，使用五点导数：y(nT)=(1/8T)[-x(nT-2T)-2x(nT-T)+2x(nT+T)+x(nT+2T)]</span>
    b = [<span class="hljs-number">-1</span>/<span class="hljs-number">4</span>,<span class="hljs-number">-1</span>/<span class="hljs-number">8</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>/<span class="hljs-number">8</span>,<span class="hljs-number">1</span>/<span class="hljs-number">4</span>];
    a = <span class="hljs-number">1</span>;
    y4 = filter(b,a,y3);
    <span class="hljs-comment">%对信号逐点平方，这使得所有数据点都为正，并且对强调较高频率(即，主要是ECG频率)的导数的输出进行非线性放大</span>
    y5 = y4.*y4; 
    <span class="hljs-comment">%滑动窗积分的目的是获得除了r波的斜率外的波形特征信息</span>
    b = [<span class="hljs-number">1</span> <span class="hljs-built_in">zeros</span>(<span class="hljs-number">1</span>,<span class="hljs-number">31</span>) <span class="hljs-number">-1</span>];
    a = [<span class="hljs-number">1</span>,<span class="hljs-number">-1</span>];
    y6 = filter(b,a,y5);

    <span class="hljs-comment">%开始画图了！</span>
    <span class="hljs-built_in">figure</span>; <span class="hljs-comment">%先画滤波效果</span>
    subplot(<span class="hljs-number">311</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),ecg(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));<span class="hljs-comment">%画图显示前2500个点</span>
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度（mV）'</span>);
    title(<span class="hljs-string">'原始信号'</span>);
    subplot(<span class="hljs-number">312</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),y1(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); <span class="hljs-comment">%先低通</span>
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度（mV）'</span>);
    title(<span class="hljs-string">'低通滤波后的信号'</span>);
    subplot(<span class="hljs-number">313</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),y3(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); <span class="hljs-comment">%再高通</span>
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度（mV）'</span>);
    title(<span class="hljs-string">'高通滤波后的信号'</span>);

    <span class="hljs-built_in">figure</span>; <span class="hljs-comment">%再画QRS检测的后续处理</span>
    subplot(<span class="hljs-number">311</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),y4(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>));
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度'</span>);
    title(<span class="hljs-string">'做差分'</span>);
    subplot(<span class="hljs-number">312</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),y5(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); 
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度'</span>);
    title(<span class="hljs-string">'做平方'</span>);
    subplot(<span class="hljs-number">313</span>);
    <span class="hljs-built_in">plot</span>(t(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>),y6(<span class="hljs-number">1</span>:<span class="hljs-number">2500</span>)); 
    xlabel(<span class="hljs-string">'时间（s）'</span>);
    ylabel(<span class="hljs-string">'幅度'</span>);
    title(<span class="hljs-string">'做积分'</span>);
</div></code></pre>
<h3 id="2-%E7%BB%93%E6%9E%9C%E5%9B%BE%E7%89%87">2. 结果（图片）</h3>
<p><strong>低高通滤波结果</strong>
<img src="低高通滤波.png" alt="">
<strong>QRS波识别预处理结果</strong>
<img src="qrs识别预处理.png" alt=""></p>

</body>
</html>
